package com.cuiweiyou.vigilesmicrecord.util.comm;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by www.gaohaiyan.com on 2017/10/28,028.
 */
public class DateFormatUtil {
    private static SimpleDateFormat sdf_yyyyMMdd_HHmmss = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
    private static SimpleDateFormat sdf_yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat sdf_HHmmss = new SimpleDateFormat("HH-mm-ss");
    private static SimpleDateFormat sdf_yyyyMM = new SimpleDateFormat("yyyy年MM月");
    private static SimpleDateFormat sdf_HHmm = new SimpleDateFormat("HH:mm");

    /**
     * 毫秒转成 年-月-日_时-分-秒
     *
     * @param millis
     * @return
     */
    public static String millis2yyyyMMddHHmmss(long millis) {
        return sdf_yyyyMMdd_HHmmss.format(new Date(millis));
    }

    /**
     * 毫秒转成 年-月-日
     *
     * @param millis
     * @return
     */
    public static String millis2yyyyMMdd(long millis) {
        return sdf_yyyyMMdd.format(new Date(millis));
    }

    /**
     * 毫秒转成 时-分-秒
     *
     * @param millis
     * @return
     */
    public static String millis2HHmmss(long millis) {
        return sdf_HHmmss.format(new Date(millis));
    }

    /**
     * 毫秒转成 时:分
     *
     * @param millis
     * @return
     */
    public static String millis2HHmm(long millis) {
        return sdf_HHmm.format(new Date(millis));
    }

    /**
     * 毫秒时间戳转为x年x月x日
     *
     * @param millis
     * @return
     */
    public static String yyyyMMdd2YMD(long millis) {
        String yyyyMMdd = sdf_yyyyMMdd.format(new Date(millis));
        return yyyyMMdd2YMD(yyyyMMdd);
    }

    /**
     * yy-MM-dd转为x年x月x日
     *
     * @param yyyyMMdd
     * @return
     */
    public static String yyyyMMdd2YMD(String yyyyMMdd) {
        StringBuilder sb = new StringBuilder();

        String[] yMd = yyyyMMdd.split("-");
        Integer year = Integer.valueOf(yMd[0]);
        Integer month = Integer.valueOf(yMd[1]);
        Integer day = Integer.valueOf(yMd[2]);

        int yearCurrent = Calendar.getInstance().get(Calendar.YEAR);
        if (yearCurrent != year.intValue()) {
            sb.append(year + "年");
        }

        sb.append(month + "月");
        sb.append(day + "日");

        return sb.toString();
    }


    /**
     * 毫秒时间戳转为x年x月
     *
     * @param millis
     * @return
     */
    public static String yyyyMMdd2YM(long millis) {
        return sdf_yyyyMM.format(new Date(millis));
    }

    public static long yyMM2Millis(String yyyyMM) {
        Date date = new Date();
        try {
            date = sdf_yyyyMM.parse(yyyyMM);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date.getTime();
    }

    /** 毫秒时长 转 时分秒时长 */
    public static String milliSecondToHHmmssDuration(long ss) {
        long secones = ss / 1000;
        return milliSecondToHHmmssDuration(secones);
    }

    public static String milliSecondToHHmmssDuration(int secones) {
        String timeStr;
        long hour = 0;
        long minute = 0;
        long second = 0;
        if (secones <= 0)
            return "00:00";
        else {
            minute = secones / 60;
            if (minute < 60) {
                second = secones % 60;
                timeStr = unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                if (hour > 99)
                    return "99:59:59";
                minute = minute % 60;
                second = secones - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return timeStr;
    }

    private static String unitFormat(long i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Long.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }
}
